Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support 'raw mangles' via leading "\1" in pragma(mangle) strings #3854

Merged
merged 3 commits into from
Oct 23, 2021

Conversation

kinke
Copy link
Member

@kinke kinke commented Oct 20, 2021

Such mangled names aren't affected by a target-specific default (C) prefix, such as _ on Mac.

@kinke
Copy link
Member Author

kinke commented Oct 20, 2021

This is a prerequisite for #3850 and enables getting hold of section start/end addresses on Mac (at least with their ld64 linker) via e.g.:

pragma(mangle, "\1section$start$__DATA$.minfo")
immutable ModuleInfo* __start___minfo;
pragma(mangle, "\1section$end$__DATA$.minfo")
immutable ModuleInfo* __stop___minfo;

@kinke kinke force-pushed the raw_mangles branch 2 times, most recently from 6b4fbc6 to 4fba4a8 Compare October 20, 2021 22:51
@Geod24
Copy link
Contributor

Geod24 commented Oct 21, 2021

What prevents this from being upstream ?

@kinke
Copy link
Member Author

kinke commented Oct 21, 2021

A DMD backend/glue layer implementation and an agreed prefix/notion, as the "\1" is AFAIK just an LLVM convention.

gen/mangling.cpp Outdated
@@ -125,11 +125,15 @@ std::string getIRMangledName(VarDeclaration *vd) {
}

std::string getIRMangledFuncName(std::string baseMangle, LINK link) {
return gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? baseMangle
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should that be a std::move? I only use C++ for LDC development at the moment, some rust creeping in… ;)

Such mangled names aren't affected by a target-specific default (C)
prefix, such as `_` on Mac.
@@ -125,11 +125,15 @@ std::string getIRMangledName(VarDeclaration *vd) {
}

std::string getIRMangledFuncName(std::string baseMangle, LINK link) {
return gABI->mangleFunctionForLLVM(std::move(baseMangle), link);
return baseMangle[0] == '\1'
? std::move(baseMangle)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

std::move is not needed here (you are returning a func param), but (I think) are needed when passing to mangleFunctionForLLVM. Yes the c++ rules are tough and I had to look it up.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was worried about the ternary expression. Seems like std::move is indeed needed to avoid a full copy: https://cpp.godbolt.org/z/1e6cPzqzW

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah interesting, good thinking.

// RUN: %ldc -output-ll -of=%t.ll %s && FileCheck %s < %t.ll

// CHECK: @"\01myGlobal" = global i32
pragma(mangle, "\1myGlobal")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: make the variable name different from the mangle (less chance for it to be accidentally correct)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right, good point, haven't thought about that possibility.

@kinke kinke merged commit a4abf30 into ldc-developers:master Oct 23, 2021
@kinke kinke deleted the raw_mangles branch October 23, 2021 12:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants